package com.sakiko.embedding.controller;

import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
import dev.langchain4j.store.embedding.EmbeddingSearchResult;
import dev.langchain4j.store.embedding.EmbeddingStore;
import io.qdrant.client.QdrantClient;
import io.qdrant.client.grpc.Collections;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import static dev.langchain4j.store.embedding.filter.MetadataFilterBuilder.metadataKey;

/**
 * projectName: com.sakiko.embedding.controller
 *
 * @author: sakiko
 * time: 2025/8/30 16:47
 * description: 文本向量化测试，看看形成向量后的文本
 */
@RestController
@Slf4j
public class EmbeddingController {
    @Resource
    EmbeddingModel embeddingModel;
    @Resource
    QdrantClient qdrantClient;
    @Resource
    EmbeddingStore<TextSegment> embeddingStore;

    // http://localhost:9012/embedding/embed
    @GetMapping("/embedding/embed")
    public String embed() {
        String prompt = """
                       咏鸡
                鸡鸣破晓光，红冠映朝阳。
                金羽披霞彩，昂首步高岗。
                """;
        Response<Embedding> embeddingResponse = embeddingModel.embed(prompt);
        System.out.println("embeddingResponse = " + embeddingResponse);
        return embeddingResponse.content().toString();
    }

    // 新建向量数据库实例和创建索引: test-qdrant
    // 类似 mysql: create database test-qdrant
    // http://localhost:9012/embedding/createcollection
    @GetMapping("/embedding/createcollection")
    public void createCollection() {
        Collections.VectorParams vectorParams = Collections.VectorParams.newBuilder()
                .setDistance(Collections.Distance.Cosine) // cos 余弦相似度
                .setSize(1024) // 文本长度
                .build();
        qdrantClient.createCollectionAsync("test-qdrant", vectorParams);
    }

    // 向向量数据库新增文本记录
    @GetMapping("/embedding/add")
    public String add() {
        String prompt = """
                       咏鸡
                鸡鸣破晓光，红冠映朝阳。
                金羽披霞彩，昂首步高岗。
                """;

        TextSegment segment = TextSegment.from(prompt);
        segment.metadata().put("author", "sakiko");
        Embedding content = embeddingModel.embed(segment).content();

        String result = embeddingStore.add(content, segment);
        System.out.println("result = " + result);
        return result; // 2ff96e0c-da74-4023-97ec-68d728b934ba
    }

    // 查找
    @GetMapping("/embedding/query1")
    public void query1() {
        Embedding content = embeddingModel.embed("咏鸡说的是什么内容").content();
        EmbeddingSearchRequest embeddingSearchRequest = EmbeddingSearchRequest.builder()
                .queryEmbedding(content)
                .maxResults(1) // 返回一条数据
                .build();

        EmbeddingSearchResult<TextSegment> res = embeddingStore.search(embeddingSearchRequest);
        System.out.println(res.matches().get(0).embedded().text());
        /* console:
                   咏鸡
            鸡鸣破晓光，红冠映朝阳。
            金羽披霞彩，昂首步高岗。
         */
    }

    @GetMapping("/embedding/query2")
    public void query2() {
        Embedding content = embeddingModel.embed("咏鸡").content();
        EmbeddingSearchRequest embeddingSearchRequest = EmbeddingSearchRequest.builder()
                .queryEmbedding(content)
                .filter(metadataKey("author").isEqualTo("sakiko")) // 过滤数据
                .maxResults(1) // 返回一条数据
                .build();

        EmbeddingSearchResult<TextSegment> res = embeddingStore.search(embeddingSearchRequest);
        System.out.println(res.matches().get(0).embedded().text()); // 没有数据 index = 0报错
        /* console:
                   咏鸡
            鸡鸣破晓光，红冠映朝阳。
            金羽披霞彩，昂首步高岗。
         */
    }

}

/*
请求: http://localhost:9012/embedding/embed
Embedding { vector = [-0.031824645, 0.12992808, -0.08460793, -0.010102294, -0.03317806, -0.037412327, 0.005109151, 0.08530397, -0.06917897, 3.156967E-5, -0.041414574, -0.033912774, -0.031109268, 0.009367582, -0.009754273, -0.0078643225, 0.050115116, -0.027010346, 0.00976394, -0.004422775, -0.033874106, 0.062798575, 0.030123206, 0.014906927, -0.051700547, 0.0295045, -0.012548113, -0.08948023, 0.045049466, -2.407754E-4, -0.08367987, 0.030625904, -0.018638492, -0.0022355556, -0.05703688, -0.044701446, -0.03470549, 0.0203786, -0.045049466, 0.050927166, 0.033139393, 0.0072697853, -0.0034246296, -0.01751709, -0.040989216, -0.028634448, -0.037160978, -0.0407572, 0.023665471, 0.01475225, -0.009261242, 0.035092182, 0.05204857, 0.03573022, 0.00567952, -0.01814546, -0.011562052, 0.005660185, -0.058931664, -0.047098927, -0.0055345106, 0.01896718, -0.05375001, -0.028286425, -0.072040476, 0.08507196, 0.064422674, 0.01747842, -0.014017538, 0.007419628, -0.012422439, 0.009053396, 0.036484268, -0.023162773, -0.047678966, 0.014085209, 0.012248429, -0.04292267, -0.011687727, -0.011523383, 0.07768616, -5.3864805E-4, -0.0068154237, 0.040718533, 0.0044735284, 0.005109151, -0.011446045, 0.00444936, -0.008439525, -0.017062727, 0.01977923, -0.009788109, 0.005283162, -0.07312322, -0.007685478, -0.020127252, -0.045977525, -9.236772E-5, 0.017294742, 0.007709646, 0.032598026, 0.01995324, 0.0045774514, 0.0154386265, 0.026623655, -0.056418173, 0.009382083, 0.033564754, -0.002801091, 0.0087633785, 0.047717635, 0.031321947, 0.005795527, -0.0027116686, 0.048259, -0.021925364, -0.01330216, -0.037876356, -0.017952116, 0.02536691, -0.045049466, 0.040409178, 0.011755398, 0.0029460997, 0.028073745, -0.051855225, 0.011330037, 0.032095328, -0.0029219317, 0.051429864, 0.0031853647, -0.030374555, -0.028112415, -0.016183006, -0.012538446, -0.026894338, -0.010808005, 0.009145236, 0.038669072, -0.02430351, 0.008357353, 0.009957286, 0.01811646, -0.0060758777, 0.041530583, -0.058119614, 0.0391331, 0.032849375, 0.021983366, 6.114547E-4, -0.0077724834, -0.023336785, 0.098760806, -0.022447396, 0.064848036, -0.03385477, -0.037760347, 0.047717635, -0.015892988, -0.016086334, -0.006535073, 0.010218302, -0.037296318, 0.030161876, 8.5192797E-4, -0.004057836, 8.6340785E-4, 0.041801266, -0.046750907, 0.01999191, -0.016424688, 0.034512147, -7.7413664E-5, -0.023452792, -0.02208004, 0.026256299, 0.008705375, 0.020455938, 0.01995324, -0.0029896025, -0.049341734, -0.04211062, -0.0023334369, -0.007168279, 0.0042874333, 0.022602072, 0.018880174, -0.015651306, -0.00895189, 0.010169965, -0.044237416, -0.055876806, 0.01549663, -0.032288674, 0.021422666, 0.011136692, 0.0023890235, -0.005718189, -0.0091114, -0.0070426045, -0.025038224, 0.02374281, -0.06248922, -0.02975585, -0.04860702, -0.010817673, -0.069759004, 0.014491234, -0.009715604, -0.041801266, -0.01995324, -0.03609758, -0.011523383, 0.02583094, 0.0066027436, -0.013166819, -0.0060565434, -0.0094642555, 0.0011014643, 0.010866009, 0.00997662, 0.0012398271, 0.06291458, 0.024013493, 0.03772168, 8.380313E-4, 0.036213584, -0.014036872, -0.012577116, -0.026623655, 0.001035606, 0.045474827, 0.008396022, -0.037450995, 0.03921044, -0.023452792, -0.06245055, 0.030084537, -0.008884219, -0.018628825, -0.009478756, -0.020455938, 0.026817001, -0.0042052614, -0.03304272, 0.016192673, -0.031012595, -0.021422666, -0.00981711, 0.005162321, 0.028112415, -0.04412141, -0.0028615112, 0.048452348, -0.005863198, -0.013360164, 0.02041727, 0.018106792, 0.056379505, 0.0044300254, 0.008700541, -0.013099148, 0.05784893, 0.059318356, 0.038553063, 0.02770639, -0.00665108, -0.045242812, 0.008628037, -0.05197123, -9.0993155E-4, -0.04922573, -0.0030693575, 0.018764166, 0.04922573, 0.025038224, 0.014423563, -0.051468533, 0.04953508, 0.013002475, 0.018628825, 0.047562957, 0.012905803, -0.01914119, 0.0061145467, -0.04446943, -0.004739378, -0.011803734, 0.0058535305, -0.035324197, -0.0046644565, 0.0019104938, 0.03155396, -0.025676263, 0.023182107, 0.0067815883, 0.0337581, -0.17818707, -0.071576454, 0.0033376242, 0.04659623, 7.697562E-4, -0.004340603, 0.02664299, -0.006201552, 0.022969428, -0.07726081, 0.055142093, -0.019682556, -0.027996408, 0.009807443, 0.015999328, 0.027919069, -0.0064529013, -0.04319335, -0.026971677, -0.027899735, -0.024361515, 0.02583094, 0.02600495, 1.0248814E-4, -0.06074911, 0.030451894, 0.012393437, 0.0066462466, -0.030335886, -0.003472966, 0.011910073, -0.018242134, -0.0187835, -0.016357018, 0.034028783, 0.063494615, 0.0106823305, 0.005278328, 0.0011407376, -0.020881299, 0.0137178525, 0.07834354, -0.022196047, 0.0094980905, -0.036039576, -0.018406479, -0.009701103, -0.0064384, -7.0994E-4, -0.03942312, -0.015206612, 3.410733E-4, 0.032501355, -0.0023394788, -0.03435747, 0.006849259, 0.0063852305, 0.015554634, 0.033313405, 0.013418168, -0.0042197625, -0.06828958, -0.0060372087, 0.013089481, -0.06028508, 0.014597574, -0.016908051, 0.023665471, 0.032114662, 5.0511473E-4, 0.03615558, 0.004301934, 0.008318684, 0.0070329374, 0.008222011, 0.019556882, 0.01602833, -0.03845639, -0.05672753, -0.040950548, 0.052667275, 0.014346225, -0.01104002, 0.06295325, -0.014926261, -0.0520099, -0.02565693, 0.024458187, 0.0133891655, 0.2262914, 0.017787773, 0.064384006, -0.020997306, 9.3289133E-4, -0.0072021144, -0.011861738, 0.0028977634, 0.043270692, -0.02933049, 0.01163939, 0.0039949985, -0.033487417, -0.018667495, 0.013466504, 0.043077346, 0.0038306548, -0.0520099, 0.04129857, 0.01238377, 0.0046668737, 0.017459085, 0.0037533166, 9.413502E-4, -0.048220333, -0.005911534, -0.030567901, -0.020823294, 0.0031321947, 0.046402887, -0.04609353, 0.016743708, 0.008120505, 0.0087198755, -0.0021135064, 0.040061157, 0.029079141, -0.011803734, 0.02208004, -0.01726574, -0.017343078, -0.010595325, -0.033738766, -0.03917177, -0.019121855, -0.00907273, -0.027049014, -0.068366915, -0.010624328, 0.005781026, 0.0031853647, -0.0011516133, -0.007603306, 0.0047562956, 0.034879502, -0.011968077, -0.0056456844, -0.035266194, 0.036484268, -0.013524507, 0.0016615617, 0.003364209, -0.02229272, -0.022060705, 0.006249889, 0.034086786, 0.017894113, -0.05286062, 0.04334803, 0.04114389, 0.012954139, -0.013447169, 0.00824618, 0.009159736, 0.011813401, 0.039635796, 0.01259645, 0.016714705, -0.031727973, 0.03576889, -0.03573022, -0.030722577, -0.045668174, -0.034299467, -0.032385346, 0.040254503, 0.034434807, 6.3970125E-5, 0.03379677, -0.017768439, -0.012084085, -0.0021340493, -0.033216733, -0.024090832, -0.0058825323, -0.07049371, 0.01850315, 0.014230218, -0.017729769, 0.020919967, 0.026991012, 0.023510795, 0.039403785, -0.03201799, 0.08406656, -0.02122932, 0.046750907, -0.021519339, -0.011533051, -0.004761129, 0.013379498, 0.00980261, -0.015235614, 0.020668618, 0.00921774, 0.009256409, 0.034840833, -0.048800368, 0.0063417275, -0.027377702, 0.008458859, -0.008840716, -0.011774732, -0.018010119, 0.021635346, -0.009246741, -0.024844877, 0.012122754, -0.047408283, 0.055142093, -0.0055490118, -0.016627701, -0.03803103, 0.058119614, 0.010711333, -0.0106049925, 0.054330043, 0.0034149624, -0.019692224, 0.018938178, -0.027551712, 0.048065655, 0.004683791, -0.006138715, 0.010875676, -0.0023588133, 2.6494355E-4, -0.04609353, 0.014935928, -0.012480442, 0.014462233, -0.02579227, 6.1024626E-4, -0.03217267, 0.022215381, 0.033255402, -0.00257391, 0.013814526, 0.01656003, -0.023723476, -2.7204296E-4, 0.043464035, 0.016163671, 0.08940289, 0.030761246, 0.046132203, -0.013978869, -0.033216733, 0.018010119, 0.014848923, 0.016579363, -0.029620508, -0.007854655, 0.043386698, 0.0203786, 0.022563403, -0.017401082, 0.012828465, -0.06612411, 0.009570595, 0.010779004, 0.051468533, 0.02264074, -0.051893894, -0.021209985, 0.021519339, -0.011204364, -0.020281928, 0.019701893, -0.032327343, -0.042149287, 0.10788671, -0.009067897, -0.015361289, 0.08445325, -0.03149596, -3.3926067E-4, 0.024438852, -0.017546091, -0.032636695, -0.06608544, -0.019595552, -0.004011916, 0.010546989, -0.037160978, -0.005660185, 1.1623379E-4, -0.015641639, -0.0012289514, -0.0102763055, -0.05158454, 0.031225275, -0.063030586, -0.029910527, 9.3832915E-4, 0.0056650187, -0.0022947676, -0.008831049, -0.026913673, -0.024999555, 0.011185029, 0.032714035, 0.006733252, -0.06434534, -0.017662099, 0.06593077, -0.034028783, 0.03884308, 0.004115839, -0.020320596, -0.015883321, 0.02335612, -0.027126353, -0.004901305, -0.01082734, -0.012229093, 0.013853195, 0.012470775, -0.011823068, -0.013369831, -0.008410523, -0.030761246, 0.030741911, 0.031147936, -0.0019419125, -0.003514052, -0.025946947, 0.008570033, -0.011890739, -0.0133891655, -0.038649738, 0.018425813, -0.022022037, -0.0076613096, 0.014694247, 0.049496412, -0.015660973, -0.018193798, -0.047717635, 0.023336785, 0.02643031, 0.0032433684, -0.056843534, 0.024786875, 0.013108815, 0.020842629, -0.04369605, -0.028170418, 0.0110980235, -0.016898384, -0.041994613, -0.0015830151, -0.028711785, -0.09473922, 0.031051263, 0.014713582, 0.0024095664, 0.05084983, 0.013398833, -0.0030210211, 0.012722124, -0.021306658, -0.028286425, 0.011707061, 0.031147936, -0.011687727, 0.027532378, 0.008541031, 0.019286199, -0.034280133, -0.024516191, -0.024400184, -0.012209759, 0.022505399, 0.018087458, 0.024632199, 0.021093978, -0.021693349, -0.015757646, 0.0044566104, -0.008715042, -0.052589938, -0.039751805, -0.013108815, -0.0034367137, -0.018619157, 0.018271135, -0.004845718, 0.023878152, -0.012944471, 0.05997573, 8.0298743E-4, 0.0273197, -0.007748315, 0.07679678, -0.022060705, 0.026275633, -0.0021606344, 0.006085545, 0.008487861, -0.0023177275, -0.001754609, -0.046016194, -3.84576E-4, -0.042265296, -0.025946947, 0.045165475, -6.464985E-4, -0.008782713, 0.010044292, -0.022737414, -0.0038089035, -0.0049327235, -0.033139393, 0.030413223, 0.01620234, 0.013920866, 0.00948359, 0.029098475, -0.030026533, 0.033274736, 0.043580044, -0.030239213, 0.009338581, 0.050888497, -0.057732925, 0.0106049925, 0.005988872, 0.06500271, -3.3352073E-4, -0.01249011, 0.055528786, -0.037953693, 0.02975585, 0.02455486, 0.018667495, -0.010285973, -0.03559488, -0.014558905, 0.0018065707, -0.037064303, 0.030219879, -0.061793175, 0.013244157, -0.005292829, -0.024806209, 0.009299912, 0.009614098, 0.0054185037, -0.052203245, -0.058583643, 0.024535526, 0.019595552, -0.0094642555, 0.011330037, -0.01082734, -0.010517987, -0.0023418956, 0.01694672, 0.033874106, -0.017188402, -0.009357915, 0.010730667, -0.020765292, -0.030915922, -0.025676263, -0.002576327, 0.01730441, -0.023240112, 0.027513044, 0.003180531, 0.0062160534, 0.027609717, -0.010266638, -0.12304498, 0.018599823, -7.564637E-4, 0.03661961, -0.0059647043, 0.027745059, -0.059395693, -0.0050463136, 0.01549663, -0.010150631, 0.010053959, -0.0053169974, 0.05819695, -0.012248429, 0.013698518, 0.019875903, 0.016637368, 0.027145687, 9.552469E-4, -0.021983366, 0.030799914, -0.016018663, 0.0073906262, -0.007487299, -0.003298955, 0.05784893, -0.02498022, -0.018947845, -0.020223925, 0.024864214, 0.033274736, 0.0071827797, 0.0017703184, 0.023240112, 0.055103425, 0.024052162, -0.0095415935, -0.026275633, 0.04539749, -0.022602072, -0.0028880963, -0.025850274, -0.029523836, -0.030703243, 0.038379055, -0.02208004, 0.007337456, -0.043773387, -0.013292493, -0.033584088, -0.045474827, -0.046325546, 0.03252069, 0.043270692, 0.009009894, -0.016869383, -0.05324731, -0.01807779, 0.018213132, 0.022350723, -0.0013473755, 1.13968024E-4, -0.020668618, 0.03712231, -0.004084421, 0.021770688, -0.04783364, 0.04334803, -0.0016784794, 0.006728418, 0.025966281, -9.721646E-4, -0.011397709, -0.034086786, -0.02660432, 0.002434943, 0.043734718, 0.0082268445, -0.017101396, -0.020552611, 0.016937053, 0.024226174, 0.028035076, 0.036774285, -0.05993706, -0.018232467, -0.012035748, -0.011620056, -0.09280577, -0.0187835, 0.011262367, -0.036677614, 0.032095328, -0.04122123, -0.027996408, 0.004657206, 0.014626577, 0.0049593085, -0.012258096, -0.0070812735, 0.0023418956, -0.0043647713, -0.029485166, 0.025038224, -0.043773387, 0.08112771, -0.01365985, -0.0012217009, 0.028209087, 0.011977744, 0.024516191, 0.034512147, 0.014906927, 0.028035076, -0.06372663, 0.003076608, 0.015544967, -0.0047587124, 0.010953015, 0.011513716, -0.011223698, 0.029987864, -0.0077434815, -0.01507127, 0.071576454, -0.005602182, -0.005660185, 0.0010899844, 0.0036107246, -0.0013159568, 0.0031056097, -0.020107917, -0.024245508, -0.036696948, -0.016221676, 0.028634448, -0.029311156, 0.0025449083, 0.047214936, -0.034125455, -0.02310477, 0.03445414, 0.027822396, -0.0063320603, -0.024825543, -0.014916594, 0.008004498, -0.004340603, -0.0029243485, -0.013186153, 0.043966733, 0.030413223, 0.041337237, 0.015767314, 0.02122932, -0.021113312, -0.001141946, 0.019692224, -0.015457961, 0.029949196, -0.0011812192, -0.018851172, -1.0943649E-4, -0.025618259, -0.020262593, -0.029079141, 0.036039576, 0.020185255, 0.003076608, 0.011223698, -0.037450995, -0.009691436, -0.008531364, -0.05943436, 0.016453689, 0.034280133, 0.0056650187, 0.0015334703, 0.026991012, 0.009227407, 0.011243032, -0.02254407, -0.036580943, -0.011262367, -0.038379055, 0.018000452, 0.001736483, -0.022196047, 0.005863198, -0.018802837, -0.015003599, 0.021712683, -0.056031484, -0.013669517, 0.03584623, -0.012509445, 0.0278804, -0.006839592, -0.013495506, -0.0031346115, -0.019170191, -0.036600277, -0.0064915703, 5.730877E-4, 0.044044074, -0.03329407, 0.0027672553, 0.011059354, -0.01698539, -0.004461444, -0.011542718, -0.011939076, 0.02664299, -0.01596066, -0.01270279, -0.03035522, 0.004618537, -0.036696948, 0.009295078, 0.026120957, 0.0066365795, -0.0019576217, -0.006626912, -0.02869245, 0.018532151, 0.015892988, -0.034879502, 0.011339705, -0.0028542608, -0.027358368, -0.005979205, 0.01868683, 0.0011002559, -0.018957512, 0.0064335666, 0.011716728, 0.002245223, 0.06937231, -0.010450317, 0.024419518, 0.012045416, 6.3199765E-4, 0.0407572, 0.025908278] }
 */
